package demo.deep;

import org.apache.catalina.User;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("deep")

public class DeepApi {
    @Autowired
    OpenAiChatModel openAiChatModel;

    /**
     * 对话记忆
     */

    @Qualifier("MemoryChatClient")
    @Autowired
    ChatClient memoryChatClient;


    final ChatClient chatClient;

    public DeepApi(ChatClient.Builder chatClient) {
        this.chatClient = chatClient
                .defaultSystem("你叫 嘻嘻， 是一个可爱到爆的小萌妹，温柔体贴，善解人意，喜欢倾听，并且会引导话题继续聊下去")//这里可以配置默认角色
//                advisor 在api前后对请求进行过滤和修改 这个是spring提供的日志advisor
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .build();
    }

    @RequestMapping(value = "memory", produces = "text/html;charset=utf-8")
    public Flux<String> memory(@RequestParam("msg") String msg, @RequestParam("id") String id) {
        return memoryChatClient.prompt().user(msg)
                //将对话按id储存
                .advisors(spc -> spc.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, id))
                .stream().content();
    }

    @RequestMapping("cm")
    public String chatModel(String message) {
        //系统提示词
        SystemMessage systemMessage = new SystemMessage("你叫hhh");
        UserMessage userMessage = new UserMessage(message);
        Prompt prompt = new Prompt(systemMessage, userMessage);
        return openAiChatModel.call(prompt).getResult().getOutput().getText();
    }


    //流式输出
    @RequestMapping(value = "flux", produces = "text/html;charset=utf-8")
    public Flux<String> flux(String msg) {
        return chatClient.prompt()
                .user(msg)
                .stream()
                .content();
    }

    @RequestMapping("chat1")
    public String chat1(String msg) {
        return this.chatClient.prompt()
                .user(msg) //发送给模型的信息
                .call() //将信息发送给模型
                .content(); //接收到模型的响应
    }

    public record Recipe(String dish, List<String> ingredients, String operate) {
    }

    //结构化输出
    @RequestMapping("entity")
    public String entity(String msg) {
        Recipe recipe = chatClient.prompt()
                .user(String.format("给我生成%s这个菜品所需要的食材和制作过程", msg))
                .call()
                .entity(Recipe.class);
        return recipe.toString();
    }

    @Autowired
    ChatModel chatModel;

    @RequestMapping("chat2")
    public String chat2(String msg) {
        return chatModel.call(msg);
    }

    @RequestMapping(value = "chat3", produces = "text/html;charset=utf-8")
    public Flux<String> chat3(String msg) {
        System.out.println(chatModel.getDefaultOptions().toString());
        return chatModel.stream(msg);
    }

    @RequestMapping("chat")
    public String chat(String msg) {
        return openAiChatModel.call(msg);
    }
}
